using Infrastructure.Constant;
using Infrastructure.Model;
using JTZY.Admin.WebApi.Filters;
using JTZY.Model;
using JTZY.Model.Financial;
using JTZY.Model.Financial.Dto;
using JTZY.Repository;
using JTZY.Service.Business.IBusinessService;
using JTZY.Service.Financial.IFinancialService;
using Microsoft.AspNetCore.Mvc;

//创建时间：2024-11-02
namespace JTZY.Admin.WebApi.Controllers.Financial
{
    /// <summary>
    /// 提现记录
    /// </summary>
    [Verify]
    [Route("financial/Corecmsusertocash")]
    public class CorecmsusertocashController : BaseController
    {
        /// <summary>
        /// 提现记录接口
        /// </summary>
        private readonly ICorecmsusertocashService _CorecmsusertocashService;
        private readonly IRedisOperationRepository _redisOperationRepository;

        public CorecmsusertocashController(ICorecmsusertocashService CorecmsusertocashService, IRedisOperationRepository redisOperationRepository)
        {
            _CorecmsusertocashService = CorecmsusertocashService;
            _redisOperationRepository = redisOperationRepository;
        }

        /// <summary>
        /// 查询提现记录列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsusertocash:list")]
        public IActionResult QueryCorecmsusertocash([FromQuery] CorecmsusertocashQueryDto parm)
        {
            parm.UserId= HttpContext.GetUId();
            var response = _CorecmsusertocashService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询提现记录详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "corecmsusertocash:query")]
        public IActionResult GetCorecmsusertocash(int Id)
        {
            var response = _CorecmsusertocashService.GetInfo(Id);
            
            var info = response.Adapt<CorecmsusertocashDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加提现记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsusertocash:add")]
        [Log(Title = "提现记录", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsusertocash([FromBody] CorecmsusertocashDto parm)
        {
            var modal = parm.Adapt<Corecmsusertocash>().ToCreate(HttpContext);

            var response = _CorecmsusertocashService.AddCorecmsusertocash(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost("tocash")]
        [ActionPermissionFilter(Permission = "corecmsusertocash:add")]
        [Log(Title = "提现记录", BusinessType = BusinessType.INSERT)]
        public async Task<IActionResult> Tocash([FromBody] CorecmsusertocashDto parm)
        {
            //var modal = parm.Adapt<Corecmsusertocash>().ToCreate(HttpContext);
            long userId = HttpContext.GetUId();
            parm.CreateTime = DateTime.Now;
            var lockKey = "LOCK_Tocash:UserId_" + userId;
            var lockHolder = Guid.NewGuid().ToString("N"); //锁持有者
            var redisUserLock = _redisOperationRepository.LockTake(lockKey, lockHolder, TimeSpan.FromSeconds(10));
            if (redisUserLock)
            {
                var response = await _CorecmsusertocashService.Tocash(userId, parm);
                if (!response.status)
                {
                    return ToResponse(ResultCode.FAIL, response.msg);

                }
                return SUCCESS(response.data);
            }
            else
            {
                return ToResponse(ResultCode.FAIL, "当前请求太频繁_请稍后再试");
            }
        }

        /// <summary>
        /// 更新提现记录
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "corecmsusertocash:edit")]
        [Log(Title = "提现记录", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCorecmsusertocash([FromBody] CorecmsusertocashDto parm)
        {
            var modal = parm.Adapt<Corecmsusertocash>().ToUpdate(HttpContext);
            var response = _CorecmsusertocashService.UpdateCorecmsusertocash(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除提现记录
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "corecmsusertocash:delete")]
        [Log(Title = "提现记录", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCorecmsusertocash([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CorecmsusertocashService.Delete(idArr));
        }

        /// <summary>
        /// 导出提现记录
        /// </summary>
        /// <returns></returns>
        [Log(Title = "提现记录", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "corecmsusertocash:export")]
        public IActionResult Export([FromQuery] CorecmsusertocashQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _CorecmsusertocashService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "提现记录", "提现记录");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 删除提现记录
        /// </summary>
        /// <returns></returns>
        [HttpGet("lastone")]
        [ActionPermissionFilter(Permission = "corecmsusertocash:lastone")]
        [Log(Title = "最后一笔提现记录", BusinessType = BusinessType.OTHER)]
        public IActionResult LastOneCorecmsusertocash()
        {
            long userId = HttpContext.GetUId();
            //userId = 50;
            var response= _CorecmsusertocashService.GetLastCashInfo(userId);
            var info = response.Adapt<CorecmsusertocashDto>();
            return SUCCESS(info);
        }
    }
}